<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="./../layui/css/layui.css" media="all">
    <script src="./../layui/layui.js"></script>
    <script src="./../js/jquery-1.8.3.min.js"></script>
    <script src="./../js/env.js"></script>
    <script>
        if (layui.sessionData('user').userInfo === undefined) {
            window.location.href = "../html/login.html"
        }
    </script>
    <title>代理管理</title>
    <style>
        .demoTable {
            margin: 10px 0 2px 10px;
        }
    </style>
</head>

<body>
<fieldset class="layui-elem-field layui-field-title">
    <legend>代理商管理</legend>
</fieldset>
<div class="demoTable">
    <button type="button" class="layui-btn" style="margin-left: 10px;margin-bottom: 10px"
            onclick="javascript:window.location.href='./add/agent_add.html'">添加代理商
    </button>
</div>
<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-inline">
                <input type="text" id="account" name="account" lay-verify="required" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" id="username" name="username" lay-verify="required" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">开始时间</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="startDate" placeholder="yyyy-MM-dd HH:mm:ss">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结束时间</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="endDate" placeholder="yyyy-MM-dd HH:mm:ss">
            </div>
        </div>
        <button class="layui-btn" onclick="sech()" data-method="offset" type="button">搜索</button>
        <button type="reset" onclick="chong()" data-method="offset" class="layui-btn">重置</button>
    </div>
</form>


<script type="text/html" id="barDemo">
    <button lay-event="edit" class="layui-btn layui-btn-sm layui-btn-normal" style="background: dodgerblue">编辑
    </button>&nbsp;
    <button type="button" onclick="deleteById('{{d.id}}')" class="layui-btn layui-btn-sm layui-btn-normal"
            style="background: red">删除
    </button>

    <!--等于0的时候就是启用  按钮显示禁用-->
    {{#  if(d.isDisable == 0 ){ }}
    <button onclick="jinyong('{{d.id}}')" type="button" class="layui-btn layui-btn-sm layui-btn-normal"
            style="background:dodgerblue">禁用
    </button>&nbsp;
    {{#  } else { }}
    <button onclick="qiyong('{{d.id}}')" type="button" class="layui-btn layui-btn-sm layui-btn-normal"
            style="background:dodgerblue">启用
    </button>&nbsp;

    {{#  } }}
</script>

<table id="demo" lay-filter="test"></table>
</body>

</html>
<script>
    layui.use(['form', 'layedit', 'laydate', 'table'], function () {
        var form = layui.form
            , $ = layui.jquery
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , table = layui.table;
//执行一个laydate实例
        laydate.render({
            elem: '#startDate' //指定元素
            , type: 'datetime'
        });
        //执行一个laydate实例
        laydate.render({
            elem: '#endDate' //指定元素
            , type: 'datetime'
        });
        var whereData = {}
        if ($("#startDate").val() != null && $("#startDate").val() != "") {
            whereData.startDate = $("#startDate").val();
        } else {
            whereData.startDate = "";
        }
        if ($("#endDate").val() != null && $("#endDate").val() != "") {
            whereData.endDate = $("#endDate").val();
        } else {
            whereData.endDate = "";
        }
        whereData.account = $("#account").val();
        whereData.username = $("#username").val();


        //第一个实例
        table.render({
            elem: '#demo',
            headers: {sessionId: getToken()}
            , height: 312
            , where: whereData
            , url: abc() + '/admin/agentUser/getAgentUsers' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {field: 'id', title: 'id', sort: true, fixed: 'left', align: 'center'}
                , {field: 'account', title: '账号', align: 'center'}
                , {field: 'username', title: '用户名', align: 'center'}
                , {field: 'createTime', title: '创建时间', align: 'center'}
                , {field: 'lastLoginTime', title: '最后登陆时间', align: 'center'}
                , {field: 'timestamp', title: '修改时间', align: 'center'}
                , {
                    field: 'isDisable', title: '是否禁用', templet: function (val) {
                        if (val.isDisable == undefined) {
                            return "";
                        }
                        ;
                        if (val.isDisable == 0) {
                            return "启用";
                        }
                        ;
                        if (val.isDisable == 1) {
                            return "禁用";
                        }
                    }, align: 'center'
                }
                , {field: 'operation', title: '操作', width: 200, align: 'center', toolbar: '#barDemo'}

            ]]
            , id: 'testReload'
            , request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , done: function (res, curr, count) {
                if (res.code === 100) {
                    layer.msg("登录失效，请重新登录")
                    window.location.href = "../html/login.html"
                    return;
                }
            }
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code !== 0) {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg //解析提示文本
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                }

            }
        });


        table.on('tool(test)', function (obj) {
            var layEvent = obj.event;
            if (layEvent == "edit") {
                var data = obj.data;
                //示范一个公告层
                layer.open({
                    type: 2,
                    title: '编辑代理商',
                    shadeClose: false,
                    area: ['470px', '470px'],
                    content: './edit/agent_edit.html',
                    success: function (layero, index) {
                        var iframeWin = window[layero.find
                        ('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        iframeWin.$("#account").val(data.account)
                        iframeWin.$("#username").val(data.username)
                        iframeWin.$("#password").val(data.password)
                        iframeWin.$("#id").val(data.id)
                        //渲染一下form表单
                        iframeWin.layui.form.render('select')

                    }
                });
            }
        });
    });

</script>
<script>
    //搜索
    function sech() {

        var whereData = {};
        if ($("#startDate").val() != null && $("#startDate").val() != "") {
            whereData.startDate = $("#startDate").val();
        } else {
            whereData.startDate = "";
        }
        if ($("#endDate").val() != null && $("#endDate").val() != "") {
            whereData.endDate = $("#endDate").val();
        } else {
            whereData.endDate = "";
        }
        whereData.account = $("#account").val();
        whereData.username = $("#username").val();
        //表刷新方法
        var table = layui.table;
        table.reload('testReload', {
            url: abc() + '/admin/agentUser/getAgentUsers'
            , where: whereData
        });

    }

    //把重置添加一个点击事件点击重置刷新页面
    function chong() {
        window.location.reload();
    }

    //删除
    function deleteById(id) {
        layer.confirm('真的要删除吗?', function (index) {

            var requestData = {
                "id": id
            };
            var responseData = MyAjax("POST", "/admin/agentUser/delAgentUser", requestData, getToken())
            if (responseData.code === 0) {
                //缓存用户登录信息
                // layui.sessionData('user', {
                //     key: 'userInfo'
                //     , value: responseData.data
                // });
                window.location.href = "./agent.html"
            }

        });
    }

    //禁用
    function jinyong(id) {
        var requestData = {
            "id": id,
            "isDisable": 1
        };
        var responseData = MyAjax("POST", "/admin/agentUser/disableAgentUser", requestData, getToken())
        if (responseData.code === 0) {
            //缓存用户登录信息
            // layui.sessionData('user', {
            //     key: 'userInfo'
            //     , value: responseData.data
            // });
            layer.msg("禁用成功")
            window.location.href = "./agent.html"
        }
    }

    //启用
    function qiyong(id) {
        var requestData = {
            "id": id,
            "isDisable": 0
        };
        var responseData = MyAjax("POST", "/admin/agentUser/disableAgentUser", requestData, getToken())
        if (responseData.code === 0) {
            //缓存用户登录信息
            // layui.sessionData('user', {
            //     key: 'userInfo'
            //     , value: responseData.data
            // });
            layer.msg("启用成功")
            window.location.href = "./agent.html"
        }
    }


</script>